package cn.hx.mall.permissions.controller;

import cn.hx.mall.permissions.entity.Menu;
import cn.hx.mall.permissions.entity.ResourceCategory;
import cn.hx.mall.permissions.entity.Resources;
import cn.hx.mall.permissions.entity.Role;
import cn.hx.mall.permissions.entity.page.Page;
import cn.hx.mall.permissions.service.MenuService;
import cn.hx.mall.permissions.service.ResourceCategoryService;
import cn.hx.mall.permissions.service.ResourcesService;
import cn.hx.mall.permissions.service.RoleService;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 后台用户角色表(Role)表控制层
 *
 * @author makejava
 * @since 2022-11-02 17:02:39
 */
@Controller
@RequestMapping("/permissions")
public class RoleController {

    @Resource
    private RoleService roleService;

    @Resource
    private MenuService menuService;

    @Resource
    private ResourcesService resourcesService;
    @Resource
    private ResourceCategoryService resourceCategoryService;


    //    角色列表
    @GetMapping("/role")
    public String role(Model model, Page page){

        Role role = new Role();
        role.setName(page.getSearch());
        List<Role> roles = roleService.queryAllByLimit(role,page.getPage(), page.getLimit());
        Page page1 = roleService.getPageInfo(page);
        model.addAttribute("roleList", roles);
        model.addAttribute("pageInfo", page1);
        return "permissions/role";
    }

    //模糊查询角色列表
    @PostMapping("/role")
    public String postRole(Model model, String sname) {
        List<Role> roles;
        if (sname != null && !sname.equals("")) {
            roles = roleService.queryLikeName(sname);
        } else {
            roles = roleService.queryAllByLimit(new Role(),0, 10);
        }
        model.addAttribute("roleList", roles);
        return "permissions/role";
    }

    //    进入菜单分类页面
    @GetMapping("/allocMenu/{id}")
    public String allocMenu(@PathVariable Integer id, Model model) {
        List<Menu> list = menuService.queryAll();
        List<Integer> mList = menuService.queryRoleMenuByRoleId(id);
        model.addAttribute("menuList", list);
        model.addAttribute("roleMenuList", mList);
        model.addAttribute("roleId", id);
        return "permissions/allocMenu";
    }

    //    保存菜单分类修改
    @SuppressWarnings("unchecked")
    @PostMapping("saveAllocMenu")
    @Transactional
    public String saveAllocMenu(String ids, Integer roleId, Model model) {
        JSONObject id = JSONObject.parseObject(ids);
        List<String> ids1 = (List<String>) id.get("ids");
        menuService.deleteMenuByRoleId(roleId);
        menuService.allocMenu(roleId, ids1);
        List<Menu> list = menuService.queryAll();
        List<Integer> mList = menuService.queryRoleMenuByRoleId(roleId);
        model.addAttribute("menuList", list);
        model.addAttribute("roleMenuList", mList);
        model.addAttribute("roleId", roleId);
        return "permissions/allocMenu";
    }

    //    进入资源分类页
    @GetMapping("/allocResource/{id}")
    public String allocResource(@PathVariable Integer id, Model model) {
        List<ResourceCategory> resourceCategories = resourceCategoryService.selectAll();
        List<Resources> list = resourcesService.selectAll();
        List<Integer> mList = resourcesService.queryRoleResourceByRoleId(id);
        model.addAttribute("resourceCategories", resourceCategories);
        model.addAttribute("resourceList", list);
        model.addAttribute("roleResourceList", mList);
        model.addAttribute("roleId", id);
        return "permissions/allocResource";
    }

    //    保存资源分类修改
    @SuppressWarnings("unchecked")
    @PostMapping("/saveAllocResource")
    @Transactional
    @ResponseBody
    public String saveAllocResource(String ids, Integer roleId) {
        System.out.println(111);
        JSONObject id = JSONObject.parseObject(ids);
        List<String> ids1 = (List<String>) id.get("ids");
        roleService.deleteResourcesByRoleId(roleId);
        roleService.allocResources(roleId, ids1);
        return "200";
    }

    //    根据id获取某个角色
    @GetMapping("/getRole/{id}")
    @ResponseBody
    public Object getRole(@PathVariable Integer id) {
        return roleService.queryById((long) id);
    }

    //   根据id删除某个角色（假删除）
    @GetMapping("role/deleteById/{id}")
    public String deleteById(@PathVariable Long id) {
        Role role = new Role();
        role.setId(id);
        role.setStatus(0);
        roleService.update(role);
//        roleService.deleteById(id);//从数据库删除
        return "redirect:/permissions/role";
    }

//    根据id修改角色状态
    @GetMapping("role/updateStatus/{id}")
    public String updateStatus(@PathVariable Long id) {
        Role role1 = roleService.queryById(id);
        if (role1.getStatus() == 1) {
            role1.setStatus(0);
        } else {
            role1.setStatus(1);
        }
        roleService.update(role1);
        return "redirect:/permissions/role";
    }

//    增加或修改角色信息
    @PostMapping("role/editRole")
    public String editRole(Role role, Integer roleId) {
        role.setId((long) roleId);
        if (roleId != 0) {
            roleService.update(role);
        } else {
            role.setAdminCount(0);
            role.setCreateTime(new Date());
            roleService.insert(role);
        }
        return "redirect:/permissions/role";
    }


}